package com.wm.blog_admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_admin.dao.TFeedbackDAO;
import com.wm.blog_admin.query.TFeedbackQuery;
import com.wm.blog_common.base.BeanUtils;
import com.wm.blog_common.bean.PageHelper;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.domain.TFeedbackDO;
import com.wm.blog_common.entity.TFeedback;
import com.wm.blog_common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 反馈表 服务实现类
 * </p>
 *
 * @author wm_yu
 * @since 2020-03-24
 */
@Service
public class TFeedbackService{
    @Autowired
    private TFeedbackDAO tFeedbackDAO;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Result<TFeedbackDO> selectById(Long id){
        return Result.success(BeanUtils.copyProperties(tFeedbackDAO.getById(id),TFeedbackDO.class));
    }

    /**
    * 根据ID删除
    * @param id
    * @return
    */
    public Result deleteById(Long id){
        return tFeedbackDAO.removeById(id)?Result.success():Result.failure();
    }

    /**
    * 添加
    * @param tFeedback
    * @return
    */
    public Result insert(TFeedback tFeedback){
        return tFeedbackDAO.save(tFeedback)?Result.success():Result.failure();
    }

    /**
    * 修改
    * @param tFeedback
    * @return
    */
    public Result update(TFeedback tFeedback){
        return tFeedbackDAO.updateById(tFeedback)?Result.success():Result.failure();
    }

    /**
    * 统计总数
    *
    * @param query
    * @return
    */
    public Result<Integer> count(TFeedbackQuery query) {
        return Result.success(tFeedbackDAO.count(this.combineQuery(query)));
    }

    /**
    * 列表查询
    * @param query
    * @return
    */
    public Result<List<TFeedbackDO>> list(TFeedbackQuery query){
        return Result.success(BeanUtils.copyListProperties(tFeedbackDAO.list(this.combineQuery(query)),TFeedbackDO.class));
    }
    /**
    * 分页查询
    * @param page
    * @return
    */
    public Result<Page<TFeedbackDO>> page(QueryPage page, TFeedbackQuery query){
        return Result.success(PageHelper.newPage(tFeedbackDAO.page(page,this.combineQuery(query)),TFeedbackDO.class));
    }

    /**
    * 组装查询条件
    *
    * @param query
    * @return
    */
    private QueryWrapper<TFeedback> combineQuery(TFeedbackQuery query) {
        QueryWrapper<TFeedback> wrapper = new QueryWrapper<>();
        //todo 查询条件 wrapper.eq("字段名","值")
        return wrapper;
    }

}







